﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using ZenSystemService.Api.Data;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.Entities;


namespace ZenSystemService.Api.Services
{
    public class TraineeOrderService
    {
        private readonly ApplicationDbContext _context;

        public TraineeOrderService(ApplicationDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取学员课程剩余数
        /// </summary>
        /// <param name="CourseTypeId"></param>
        /// <param name="TraineeId"></param>
        /// <returns></returns>
        public async Task<List<TraineeCourseSurplus>> GetTraineeCourseSurplus(int CourseTypeId, int TraineeId)
        {
            try
            {
                return await _context.TraineeCourseSurpluss.Where(item => item.CourseTypeId == CourseTypeId && item.TraineeId == TraineeId).ToListAsync();
            }
            catch
            {
                return null;
            }
        }

        public async Task<TraineeCourseSurplus> GetTraineeCourseSurplus(int CourseTypeId, int TraineeId,int? CoachId)
        {
            try
            {
                return await _context.TraineeCourseSurpluss.FirstOrDefaultAsync(item => item.CourseTypeId == CourseTypeId && item.TraineeId == TraineeId && item.CoachId == CoachId);
            }
            catch
            {
                return null;
            }
        }

        public async Task<List<TraineeCourseSurplus>> GetTraineeCourseSurplusByCourseTypeIdAsync(int CourseTypeId)
        {
            try
            {
                return await _context.TraineeCourseSurpluss.Where(item => item.CourseTypeId == CourseTypeId).ToListAsync();
            }
            catch
            {
                return null;
            }
        }

        public List<TraineeCourseSurplus> GetTraineeCourseSurplusByCourseTypeId(int CourseTypeId)
        {
            try
            {
                return _context.TraineeCourseSurpluss.Where(item => item.CourseTypeId == CourseTypeId).ToList();
            }
            catch
            {
                return null;
            }
        }

        public List<TraineeCourseSurplus> GetTraineeCourseSurplusByCoachId(int CoachId)
        {
            try
            {
                return _context.TraineeCourseSurpluss.Where(item => item.CoachId == CoachId).ToList();
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获取学员课程剩余数
        /// </summary>
        /// <param name="TraineeId"></param>
        /// <returns></returns>
        public async Task<List<TraineeCourseSurplus>> GetTraineeCourseSurplus(int TraineeId)
        {
            try
            {
                return await _context.TraineeCourseSurpluss.Where(item => item.TraineeId == TraineeId).ToListAsync();
            }
            catch
            {
                return null;
            }
        }



        public async Task<bool> UpdateTraineeCourseSurplusAsync(TraineeCourseSurplus _traineeCourseSurplus)
        {
            _context.Entry(_traineeCourseSurplus).State = EntityState.Modified;
            int count = await _context.SaveChangesAsync();
            return count > 0 ? true : false;
        }

        public bool AddTraineeCourseSurplusAsync(ref TraineeCourseSurplus _traineeCourseSurplus)
        {
            _context.TraineeCourseSurpluss.Add(_traineeCourseSurplus);
            int count = _context.SaveChanges();
            return count > 0 ? true : false;
        }


        /// <summary>
        /// 创建学员订单
        /// </summary>
        /// <param name="traineeOrder"></param>
        /// <returns></returns>
        public bool CreateTraineeOrder(ref TraineeOrder traineeOrder)
        {
            _context.TraineeOrders.Add(traineeOrder);
            int count = _context.SaveChanges();
            return count > 0 ? true : false;
        }


        /// <summary>
        /// 获取学员订单实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<TraineeOrder> GetTraineeOrderInfo(int id)
        {
            return await _context.TraineeOrders.SingleOrDefaultAsync(item => item.TraineeOrderId == id);
        }

        /// <summary>
        /// 获取学员订单列表
        /// </summary>
        /// <param name="InstitutionId"></param>
        /// <param name="PaymentStatus"></param>
        /// <returns></returns>
        public IEnumerable<TraineeOrder> GetTraineeOrderList(int page, int pageSize, int InstitutionId,int? PaymentStatus, out int recordCount)
        {
            IEnumerable<TraineeOrder> TraineeOrderList = _context.TraineeOrders.Where(item => item.InstitutionId == InstitutionId ).OrderByDescending(item=>item.CreatedDate);
            if (PaymentStatus.HasValue)
            {
                TraineeOrderList = TraineeOrderList.Where(item => item.PaymentStatus == PaymentStatus);
            }
            recordCount = TraineeOrderList == null ? 0 : TraineeOrderList.Count();
            if (page == 0 || pageSize == 0)
            {
                return TraineeOrderList;
            }
            else
            {
                TraineeOrderList = TraineeOrderList.Skip((page - 1) * pageSize).Take(pageSize);
            }
            return TraineeOrderList;
        }

        public IEnumerable<TraineeOrder> GetTraineeOrderList(int traineeid)
        {
            IEnumerable<TraineeOrder> TraineeOrderList = _context.TraineeOrders.Where(item => item.TraineeId == traineeid).OrderByDescending(item => item.CreatedDate);
            return TraineeOrderList;
        }


        /// <summary>
        /// 得到销售自己的销售学员订单
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="InstitutionId"></param>
        /// <param name="PaymentStatus"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IEnumerable<TraineeOrder> GetSalesOwnTraineeOrderList(int page, int pageSize, int salesUserId,out int recordCount)
        {
            IEnumerable<TraineeOrder> TraineeOrderList = _context.TraineeOrders.Where(item => item.SalesUserId == salesUserId).OrderByDescending(item => item.CreatedDate);
          
            recordCount = TraineeOrderList == null ? 0 : TraineeOrderList.Count();
            if (page == 0 || pageSize == 0)
            {
                return TraineeOrderList;
            }
            else
            {
                TraineeOrderList = TraineeOrderList.Skip((page - 1) * pageSize).Take(pageSize);
            }
            return TraineeOrderList;
        }


        public async Task<bool> UpdateTraineeOrderAsync(TraineeOrder _traineeOrder)
        {
            _context.Entry(_traineeOrder).State = EntityState.Modified;
            int count = await _context.SaveChangesAsync();
            return count > 0 ? true : false;
        }

        public bool CreateTraineeOrderDetail(ref TraineeOrderDetail traineeOrderDetail)
        {
            _context.TraineeOrderDetails.Add(traineeOrderDetail);
            int count = _context.SaveChanges();
            return count > 0 ? true : false;
        }

        public async Task<List<TraineeOrderDetail>> GetTraineeOrderDetailList(int TraineeOrderId)
        { 
            return _context.TraineeOrderDetails.Where(item=>item.TraineeOrderId== TraineeOrderId).ToList();
        }


        public async Task<bool> AddTraineeOrderRecommendTermDetail(TraineeOrderRecommendTermDetail _traineeOrderRecommendTermDetail)
        {
            _context.TraineeOrderRecommendTermDetails.Add(_traineeOrderRecommendTermDetail);
            int count = await _context.SaveChangesAsync();
            return count > 0 ? true : false;
        }

        public async Task<List<TraineeOrderRecommendTermDetail>> GetTraineeOrderRecommendTermDetailList(int TraineeOrderDetailId)
        {
            try
            {
                return _context.TraineeOrderRecommendTermDetails.Where(item => item.TraineeOrderDetailId == TraineeOrderDetailId).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
